package com.oocl.ita.sakura.ioutil;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Collection;

import com.oocl.ita.sakura.client.Client;
import com.oocl.ita.sakura.server.IMServer;

public class NetIOUtil {
	
	// send 既处理发单人也处理发多人
	public static void send(Object obj, Client toClient) {
		if ("all".equals(toClient.getAccout().toLowerCase())) {
			sendAll(obj);
		} else {
			ObjectOutputStream toOos = IMServer.getOos(toClient);
			if (toOos != null) {
				try {
					toOos.writeObject(obj);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				System.out.println("NetIOUtil : " + Thread.currentThread().getName() + " can't find coresponding Oos");
			}
		}
	}
	
	private static void sendAll(Object obj) {
		Collection< ObjectOutputStream> oosCollection = IMServer.getClientOosMap().values();
		System.out.println("NetIOUtil : " + Thread.currentThread().getName());
		System.out.println("NetIOUtil : " + oosCollection.size() + "Clients Connecting");
		for (ObjectOutputStream oos : oosCollection) {
			try {
				oos.writeObject(obj);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
